package com.blog.dao.frontpage;

import java.util.List;

import com.blog.dao.conn.BaseDao;
import com.blog.dao.conn.ParamSet;
import com.blog.entity.AdminArticleCountEntity;
import com.blog.entity.AdminArticleEntity;
import com.blog.entity.AdminArticleQueryEntity;

public class AdminArticleDao extends BaseDao{
	private static AdminArticleDao instance;
	private AdminArticleDao() {
		
	}
	public static synchronized AdminArticleDao getInstance() {
		if(instance==null) {
			instance=new AdminArticleDao();
		}
		return instance;
	}
	/**
	 * 1.查询最新的五篇文章
	 */
	public List<AdminArticleEntity> getNewAdminArticle(AdminArticleQueryEntity e){
		StringBuffer sql=new StringBuffer("select a.article_id,article_title,DATE_FORMAT(article_time,'%Y-%m-%d') publish_time,article_readnumber,article_supportnumber,user_name,user_id,types_name,count(*) article_discussnumber,discuss_time\r\n" + 
				"from tb_article a left JOIN tb_user u on a.writer_id=u.user_id\r\n" + 
				"LEFT JOIN tb_types t on a.article_typesid=t.types_id\r\n" + 
				"LEFT JOIN tb_discuss d on a.article_id=d.article_id\r\n" + 
				"where article_status=");
			sql.append(e.getStatus());
			if(e.getStarttime()!=null) {
				sql.append(" and DATE(article_time)>'").append(e.getStarttime()).append("'");
			}
			if(e.getEndTime()!=null) {
				sql.append(" and DATE(article_time)<'").append(e.getEndTime()).append("'");
			}
			if(e.getKey()!=""&&e.getKey()!=null) {
				sql.append(" and article_title like '%").append(e.getKey()).append("%'");
			}
			sql.append("  GROUP BY a.article_id order by article_time desc limit ").append(e.getStartindex()).append(",").append(e.getPageSize());
		
			return queryList(sql.toString(),AdminArticleEntity.class );
	}
	/**
	 * 2.根据状态查询文章的总篇数
	 */
	public AdminArticleCountEntity getTotalRecord(AdminArticleQueryEntity e) {
		//String sql1="select count(*) totalRecord from tb_article where article_status=?";
		StringBuffer sql=new StringBuffer("select count(*) totalRecord" + 
				" from tb_article a "+
				"where article_status=");
			sql.append(e.getStatus());
			if(e.getStarttime()!=null) {
				sql.append(" and DATE(article_time)>'").append(e.getStarttime()).append("'");
			}
			if(e.getEndTime()!=null) {
				sql.append(" and DATE(article_time)<'").append(e.getEndTime()).append("'");
			}
			if(e.getKey()!=""&&e.getKey()!=null) {
				sql.append(" and article_title like '%").append(e.getKey()).append("%'");
			}
			
	
		return query(sql.toString(),AdminArticleCountEntity.class);
	}
	
}
